public class BinaryTree {
    //创建树的节点
    public static class TreeNode {
        public char val;
        public TreeNode left;//左节点
        public TreeNode right;//右节点
        public  TreeNode(char val) {
            this.val = val;
        }
    }
    //定义为全局变量，每次++改变count的值
    static int count;
    //前序遍历创建一颗二叉树
    public TreeNode createBinaryTree(TreeNode[] tree) {
        return createPreTree(tree);
    }
    public TreeNode createPreTree(TreeNode[] tree) {
        //前序遍历时，第一个节点是根节点
        //左子树和右子树使用#分割开
        TreeNode root = null;
        //如果遍历时，计数count>数组的长度，或者遇到#，此时root是空节点。
        if(count > tree.length-1 || tree[count].val == '#') {
            root = null;
            count++;
        } else {
            root = new TreeNode(tree[count].val);
            count++;
            root.left = createPreTree(tree);
            root.right = createPreTree(tree);
        }
        return root;
    }
    public void proBinaryTree(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val+" ");
        proBinaryTree(root.left);
        proBinaryTree(root.right);
    }
    public void inBinaryTree(TreeNode root) {
        if(root == null) {
            return;
        }
        inBinaryTree(root.left);
        System.out.print(root.val+" ");
        inBinaryTree(root.right);
    }
}
